<?php
// 应用公共文件
function getweb(){
    $web = @file_get_contents('./web.log');
    if (empty($web))return '';
    return 'http://'.$web;
}

function gettemplatelist($lang){
    // 指定目录路径
    $directory = '../view/index/'.$lang;
    // 检查目录是否存在
    is_dir($directory) || mkdir($directory);
    // 扫描目录中的文件和子目录
    $files = scandir($directory);
    // 过滤出以 .html 结尾的文件
    $htmlFiles = array_filter($files, function($file) {
        return pathinfo($file, PATHINFO_EXTENSION) === 'html';
    });
    // 输出结果
    $files = [];
    foreach ($htmlFiles AS $file) {
        $html = str_replace('.html', '', $file);
        if (in_array($html,['head','footer','header']))continue;
        $files[] = $html;
    }
    return $files;
}
//树形结构
function getTree($lists, $parentId = 0, $keyParentId = 'pid', $keySonId = 'id', $keyChildren = 'son') {
    $tree = [];
    foreach($lists as $k => $v) {
        if(array_key_exists($keySonId, $v) && array_key_exists($keyParentId, $v) && $v[$keyParentId] == $parentId) {
            $v[$keyChildren] = getTree($lists, $v[$keySonId], $keyParentId, $keySonId, $keyChildren);
            $tree[] = $v;
        }
    }
    return $tree;
}

function getTreeLevel($list,$pid = 0,$level = 1){
    $tree = [];
    foreach ($list AS $key => $value){
        if ($pid == $value['pid']){
            $tree[] = array_merge($value,['level' => $level]);
            $tree = array_merge($tree,getTreeLevel($list,$value['id'],$level + 1));
        }
    }
    return $tree;
}